package com.metacube.questionbank.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.springframework.beans.factory.annotation.Autowired;

import com.metacube.questionbank.dao.TagDAO;
import com.metacube.questionbank.model.Question;
import com.metacube.questionbank.model.Tag;
import com.metacube.questionbank.service.TagService;

/*
 * @author Team Neeraj Jain
 * */
public class TagServiceImpl implements TagService {
	@Autowired
	TagDAO tagDao;

	/*
	 * This method returns all the tags that are in the database.
	 * 
	 * @return list of tags
	 */
	public List<Tag> getTags() {
		return tagDao.getTags();
	}

	/*
	 * @param tagId id of tag This method returns a list of questions that are
	 * related to a particular tag
	 * 
	 * @return list of questions.
	 */
	public List<Question> getTagQuestions(int tagId) {
		return tagDao.getTagQuestions(tagId);
	}

	/*
	 * @param tagName name of tag This method returns a list of tags according
	 * to the string that is typed by user in the search bar of tag page.
	 * 
	 * @return list of tag
	 */
	public List<Tag> getMatchedTag(String tagName) {
		return tagDao.getMatchedTag(tagName);
	}

	/*
	 * @param questions list of questions This method returns a list that
	 * contains a list of tags.For each question it adds a list of tags to the
	 * list and then it returns that list.
	 * 
	 * @return list of tag lists
	 */
	public List<List<Tag>> getQuestionTags(List<Question> questions) {
		List<List<Tag>> questionsTags = new ArrayList<List<Tag>>();
		for (int index = 0; index < questions.size(); index++)
			questionsTags.add(tagDao.getQuestionTags(questions.get(index)
					.getQuesId()));
		return questionsTags;
	}

	/*
	 * @param quesId id of question This method returns a list of tags
	 * associated with a particular question
	 * 
	 * @return list of tags.
	 */
	public List<Tag> getQuestionTags(int quesId) {
		return tagDao.getQuestionTags(quesId);
	}

	/*
	 * @param query string written by user This method returns a list of tag
	 * names that starts with the query written by the user
	 * 
	 * @return list of tag names.
	 */
	public List<String> getMatchedTagsList(String query) {
		String tagName = null;
		query = query.toLowerCase();
		List<String> matched = new ArrayList<String>();
		List<Tag> allTagsList = tagDao.getTags();
		for (int tagCounter = 0; tagCounter < allTagsList.size(); tagCounter++) {
			tagName = allTagsList.get(tagCounter).getTagName().toLowerCase();
			if (tagName.startsWith(query)) {
				matched.add(allTagsList.get(tagCounter).getTagName());
			}
		}
		return matched;
	}

	/*
	 * @param objTag object of Tag class This method inserts a tag in the
	 * database
	 */
	public void insertTag(Tag objTag) {
		tagDao.insertTag(objTag);
	}

	/*
	 * @param tagName name of tag
	 * 
	 * @param quesId id of question This method associates tags with the
	 * question if that tag exists in the tags that are stored in database.
	 */
	public void insertQuestionTags(String tagName, int quesId) {

		String[] separateWord = tagName.split(",");
		for (int index=0;index<separateWord.length;index++) {
			Tag tag = tagDao.getTag(separateWord[index]);
			if (tag != null){
				tagDao.insertQuestionTags(tag.getTagId(), quesId);
			}
		}
	}
}
